package anonymouschat.client.frames;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import anonymouschat.NickList;
import anonymouschat.client.Client;
import anonymouschat.utils.LocalFileOps;



/**
 * Frame which contains a list of users and their nicknames Allows for adding
 * and deleting of users and nicknames
 * 
 * @author Winters
 * 
 */
public class NickListFrame extends JFrame implements ActionListener,
        DocumentListener, WindowListener
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Client client;

    private JPanel textPanel;

    private JPanel buttonPanel;

    private JPanel userIDPanel;

    private JPanel nickNamePanel;

    private JPanel sendMsgPanel;

    private JPanel showHistoryPanel;

    private JPanel deletePanel;

    private NickList nickList;

    private JLabel userIDLabel = new JLabel( "UserID:" );

    private JLabel nickNameLabel = new JLabel( "Nick Name:" );

    private JButton[] sendMsgButton;

    private JButton[] showHistoryButton;

    private JButton[] deleteButton;

    private JButton addButton;

    private JTextField[] nickText;

    private JTextField[] idText;

    private List<JTextField> userIDList;

    private List<JTextField> nickNameList;

    private List<JButton> sendMsgButtonList;

    private List<JButton> showHistoryButtonList;

    private List<JButton> deleteButtonList;

    /**
     * Create NickListFrame UI
     * 
     * @param userID
     *            - the userid running the client app
     * @param nickList
     *            - if null, creates a new list
     */
    public NickListFrame( Client client ) {
        this.client = client;

        nickList = LocalFileOps.getNickNameList();
        if(nickList == null){
            nickList = new NickList();
        }
        
        setTitle( "Nickname List for : " + client.getIdentity() );
        setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        addWindowListener( this );
        setLocation( 0, 0 );
        setVisible( true );
        setResizable( false );
        setLayout( new BorderLayout() );

        textPanel = new JPanel();
        textPanel.setLayout( new GridLayout( 1, 2 ) );

        buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout( 1, 3 ) );

        // userID Panel
        userIDPanel = new JPanel();
        userIDPanel.setLayout( new GridLayout( 0, 1, 0, 0 ) );
        userIDPanel.add( userIDLabel );
        userIDPanel.add( new JSeparator( SwingConstants.HORIZONTAL ) );
        List<String> tempList = nickList.getUserIDList();
        idText = new JTextField[tempList.size()];
        userIDList = new ArrayList<JTextField>();
        for( int i = 0; i < tempList.size(); i++ ) {
            idText[i] = new JTextField();
            idText[i].setText( tempList.get( i ).toString() );
            idText[i].addActionListener( this );
            idText[i].getDocument().addDocumentListener( this );
            userIDList.add( idText[i] );
            userIDPanel.add( idText[i] );
        }
        // nickName Panel
        nickNamePanel = new JPanel();
        nickNamePanel.setLayout( new GridLayout( 0, 1, 0, 0 ) );
        nickNamePanel.add( nickNameLabel );
        nickNamePanel.add( new JSeparator( SwingConstants.HORIZONTAL ) );
        tempList = nickList.getNickNameList();
        nickText = new JTextField[tempList.size()];
        nickNameList = new ArrayList<JTextField>();
        for( int i = 0; i < tempList.size(); i++ ) {
            nickText[i] = new JTextField();
            nickText[i].setText( tempList.get( i ).toString() );
            nickText[i].addActionListener( this );
            nickText[i].getDocument().addDocumentListener( this );
            nickNameList.add( nickText[i] );
            nickNamePanel.add( nickText[i] );
        }

        // sendMsg Panel
        sendMsgPanel = new JPanel();
        sendMsgPanel.setLayout( new GridLayout( 0, 1, 0, 0 ) );
        sendMsgPanel.add( new JLabel( "" ) );
        sendMsgPanel.add( new JSeparator( SwingConstants.HORIZONTAL ) );
        sendMsgButton = new JButton[tempList.size()];
        sendMsgButtonList = new ArrayList<JButton>();
        for( int i = 0; i < tempList.size(); i++ ) {
            sendMsgButton[i] = new JButton( "Message" );
            sendMsgButton[i].addActionListener( this );
            sendMsgButtonList.add( sendMsgButton[i] );
            sendMsgPanel.add( sendMsgButton[i] );
        }

        // showHistory Panel
        showHistoryPanel = new JPanel();
        showHistoryPanel.setLayout( new GridLayout( 0, 1, 0, 0 ) );
        showHistoryPanel.add( new JLabel( "" ) );
        showHistoryPanel.add( new JSeparator( SwingConstants.HORIZONTAL ) );
        showHistoryButton = new JButton[tempList.size()];
        showHistoryButtonList = new ArrayList<JButton>();
        for( int i = 0; i < tempList.size(); i++ ) {
            showHistoryButton[i] = new JButton( "History" );
            showHistoryButton[i].addActionListener( this );
            showHistoryButtonList.add( showHistoryButton[i] );
            showHistoryPanel.add( showHistoryButton[i] );
        }

        // deleting Panel
        deletePanel = new JPanel();
        deletePanel.setLayout( new GridLayout( 0, 1, 0, 0 ) );
        deletePanel.add( new JLabel( "" ) );
        deletePanel.add( new JSeparator( SwingConstants.HORIZONTAL ) );
        deleteButton = new JButton[tempList.size()];
        deleteButtonList = new ArrayList<JButton>();
        for( int i = 0; i < tempList.size(); i++ ) {
            deleteButton[i] = new JButton( "Delete" );
            deleteButton[i].addActionListener( this );
            deleteButtonList.add( deleteButton[i] );
            deletePanel.add( deleteButton[i] );
        }

        // add button
        addButton = new JButton( "Add" );
        addButton.addActionListener( this );

        textPanel.add( userIDPanel );
        textPanel.add( nickNamePanel );
        buttonPanel.add( sendMsgPanel );
        buttonPanel.add( showHistoryPanel );
        buttonPanel.add( deletePanel );

        getContentPane().add( textPanel, BorderLayout.WEST );
        getContentPane().add( buttonPanel, BorderLayout.EAST );

        getContentPane().add( addButton, BorderLayout.SOUTH );

        pack();

        validate();
    }

    @Override
    public void actionPerformed( ActionEvent e ) {
        int i = -1;

        if( e.getSource() == addButton ) {
            nickList.getUserIDList().add( "" );
            nickList.getNickNameList().add( "" );

            JTextField tempTextField = new JTextField();
            tempTextField.getDocument().addDocumentListener( this );
            userIDList.add( tempTextField );
            userIDPanel.add( tempTextField );

            tempTextField = new JTextField();
            tempTextField.getDocument().addDocumentListener( this );
            nickNameList.add( tempTextField );
            nickNamePanel.add( tempTextField );

            JButton tempButton = new JButton( "Delete" );
            tempButton.addActionListener( this );
            deleteButtonList.add( tempButton );
            deletePanel.add( tempButton );

            tempButton = new JButton( "Message" );
            tempButton.addActionListener( this );
            sendMsgButtonList.add( tempButton );
            sendMsgPanel.add( tempButton );

            tempButton = new JButton( "History" );
            tempButton.addActionListener( this );
            showHistoryButtonList.add( tempButton );
            showHistoryPanel.add( tempButton );

            pack();
            validate();
        }

        else {
            i = deleteButtonList.indexOf( e.getSource() );
            if( i != -1 ) {
                nickList.getNickNameList().remove( i );
                nickList.getUserIDList().remove( i );

                userIDPanel.remove( userIDList.get( i ) );
                nickNamePanel.remove( nickNameList.get( i ) );
                sendMsgPanel.remove( sendMsgButtonList.get( i ) );
                showHistoryPanel.remove( showHistoryButtonList.get( i ) );
                deletePanel.remove( deleteButtonList.get( i ) );

                userIDList.remove( i );
                nickNameList.remove( i );
                sendMsgButtonList.remove( i );
                showHistoryButtonList.remove( i );
                deleteButtonList.remove( i );

                if( nickNameList.size() != 0 )
                    pack();
                validate();
                return;
            }
            i = sendMsgButtonList.indexOf( e.getSource() );
            if( i != -1 ) {
                client.getChatFrame().addChatDst(
                        userIDList.get( userIDList.indexOf( e.getSource() ) )
                                .getText() );
                return;
            }
            // }
            // handle show history
            // for( int i = 0; i < nickList.getNickNameList().size(); i++ ) {
            i = showHistoryButtonList.indexOf( e.getSource() );
            if( i != -1 ) {
                new LogFrame( userIDList.get(
                        userIDList.indexOf( e.getSource() ) ).getText() );
                return;
            }
        }
        // }
    }

    @Override
    public void changedUpdate( DocumentEvent e ) {
        textChangeDetectioner( e );
    }

    @Override
    public void insertUpdate( DocumentEvent e ) {
        textChangeDetectioner( e );
    }

    @Override
    public void removeUpdate( DocumentEvent e ) {
        textChangeDetectioner( e );
    }

    private void textChangeDetectioner( DocumentEvent e ) {

        for( int i = 0; i < nickList.getNickNameList().size(); i++ ) {
            if( e.getDocument().equals( userIDList.get( i ).getDocument() ) ) {
                nickList.getUserIDList().set( i, userIDList.get( i ).getText() );
            } else {
                if( e.getDocument()
                        .equals( nickNameList.get( i ).getDocument() ) ) {
                    nickList.getNickNameList().set( i,
                            nickNameList.get( i ).getText() );
                }
            }
        }
    }

    @Override
    public void windowActivated( WindowEvent arg0 ) {

    }

    @Override
    public void windowClosed( WindowEvent arg0 ) {

    }

    @Override
    public void windowClosing( WindowEvent arg0 ) {
        LocalFileOps.updateNicknameList( nickList );
    }

    @Override
    public void windowDeactivated( WindowEvent arg0 ) {

    }

    @Override
    public void windowDeiconified( WindowEvent arg0 ) {

    }

    @Override
    public void windowIconified( WindowEvent arg0 ) {

    }

    @Override
    public void windowOpened( WindowEvent arg0 ) {

    }

}